package com.flame.ai.pdf.bean;

import lombok.Getter;
import lombok.Setter;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;

import java.util.Base64;
import java.util.List;

/**
 * @author Jflame
 * @version 1.0.0
 * @since 2025/8/27
 */
@Getter
public class ImageItem extends DocumentItem{

    private Resource resource;

    private MimeType mimeType;

    @Setter
    private List<String> caption;

    @Setter
    private List<String> footnote;

    @Setter
    private String context;

    public void setImage(String base64DataUrl) {
        String[] parts = base64DataUrl.split(",", 2);
        String header = parts[0]; // data:image/jpeg;base64
        String base64Data = parts[1]; // 实际的base64数据

        // 提取mimeType
        String mimeTypeStr = header.substring(5, header.indexOf(";")); // 去掉"data:"前缀并截取到";"之前
        mimeType = MimeTypeUtils.parseMimeType(mimeTypeStr);

        // 解码base64数据
        byte[] imageBytes = Base64.getDecoder().decode(base64Data);
        resource = new ByteArrayResource(imageBytes);
    }
}
